package com.zw.rpc;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 表示服务
 * @author zw
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode
public class ServiceDescriptor {

    private String clazz;

    private String method;

    private String returnType;
    
    private String[] parameterTypes;

    public static ServiceDescriptor from(Class clazz, Method method) {

        Class<?>[] parameterTypes = method.getParameterTypes();
        List<String> collect = Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.toList());
        String[] strings = collect.toArray(new String[]{});
        return new ServiceDescriptor().setClazz(clazz.getName()).setMethod(method.getName())
                .setReturnType(method.getReturnType().getName()).setParameterTypes(strings);

    }

}
